﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace FinalSettlementModel
{
    public class TablePheDuyet : Helper
    {
        public int approveIdentity { set; get; }

        string query = "SELECT PD.ID,PD.HoSoId,PD.DiaDiem,PD.TGKhoiCong,PD.TGHoanThanh,PD.QuyMoDauTu,PD.GTQTDuocDuyet," +
            "PD.ChiPhiXayDung,PD.ChiPhiThietBi,PD.ChiPhiQLDA,PD.ChiPhiTVDT,PD.ChiPhiBTGPMB,PD.ChiPhiKhac,PD.TrinhKy," +
            "PD.SoLuu,PD.NgayPheDuyet,PD.VanBanPheDuyet,PD.PhiThamTra,PD.HoanThanh,PD.GhiChu," +
            "HS.ID,HS.TenDuAn,HS.MaDuAn,HS.NhomCongTrinhId,HS.QuyetDinhDauTuId,HS.ChuDauTuId,HS.NgayNhan,HS.TongDuAn," +
            "HS.TongDuToan,HS.DeNghiQuyetToan,HS.HinhThucChonThauId,HS.UserId " +
            "FROM dbo.[TblPheDuyet] AS PD " +
            "INNER JOIN dbo.[tblHoSo] AS HS ON PD.HoSoId = HS.ID";

        // Hàm lấy tất cả danh sách phê duyệt
        public DataTable ListApproveData()
        {
            return this.ExecuteDataTable(query, null);
        }

        // Hàm lấy danh sách phê duyệt đã trình ký
        public DataTable ListSignApproveData()
        {
            string _query = this.query + " WHERE PD.TrinhKy = 'True'";
            return this.ExecuteDataTable(_query, null);
        }

        // Hàm kiểm tra dữ liệu phê duyệt hồ sơ đã tồn tại hay chưa (sử dụng cho phương thức tạo mới và cập nhật)
        public bool checkApproveName(int id, int IdHoSo)
        {
            var checkApprover = from check in nc.TblPheDuyets
                                where check.ID != id && check.HoSoId == IdHoSo
                                select check;
            if (checkApprover.Any())
            {
                return false;
            }
            return true;
        }

        // Hàm tạo mới dữ liệu phê duyệt
        public void insertApprove(int HoSoId,
                                string DiaDiem,
                                DateTime TGKhoiCong,
                                DateTime TGHoanThanh,
                                string QuyMoDauTu,
                                decimal GTQTDuocDuyet,
                                decimal ChiPhiXayDung,
                                decimal ChiPhiThietBi,
                                decimal ChiPhiQLDA,
                                decimal ChiPhiTVDT,
                                decimal ChiPhiBTGPMB,
                                decimal ChiPhiKhac,
                                bool TrinhKy,
                                string SoLuu,
                                DateTime NgayPheDuyet,
                                string VanBanPheDuyet,
                                decimal PhiThamTra,
                                bool HoanThanh,
                                string GhiChu)
        {
            TblPheDuyet approve = new TblPheDuyet();
            approve.HoSoId = HoSoId;
            approve.DiaDiem = DiaDiem;
            approve.TGKhoiCong = TGKhoiCong;
            approve.TGHoanThanh = TGHoanThanh;
            approve.QuyMoDauTu = QuyMoDauTu;
            approve.GTQTDuocDuyet = GTQTDuocDuyet;
            approve.ChiPhiXayDung = ChiPhiXayDung;
            approve.ChiPhiThietBi = ChiPhiThietBi;
            approve.ChiPhiQLDA = ChiPhiQLDA;
            approve.ChiPhiTVDT = ChiPhiTVDT;
            approve.ChiPhiBTGPMB = ChiPhiBTGPMB;
            approve.ChiPhiKhac = ChiPhiKhac;
            approve.TrinhKy = TrinhKy;
            approve.SoLuu = SoLuu;
            approve.NgayPheDuyet = NgayPheDuyet;
            approve.VanBanPheDuyet = VanBanPheDuyet;
            approve.PhiThamTra = PhiThamTra;
            approve.HoanThanh = HoanThanh;
            approve.GhiChu = GhiChu;
            nc.TblPheDuyets.InsertOnSubmit(approve);
            nc.SubmitChanges();

            approveIdentity = approve.ID;
        }

        // Hàm cập nhật dữ liệu phê duyệt
        public void updateApprove(int id, int HoSoId,
                                string DiaDiem,
                                DateTime TGKhoiCong,
                                DateTime TGHoanThanh,
                                string QuyMoDauTu,
                                decimal GTQTDuocDuyet,
                                decimal ChiPhiXayDung,
                                decimal ChiPhiThietBi,
                                decimal ChiPhiQLDA,
                                decimal ChiPhiTVDT,
                                decimal ChiPhiBTGPMB,
                                decimal ChiPhiKhac,
                                bool TrinhKy,
                                string SoLuu,
                                DateTime NgayPheDuyet,
                                string VanBanPheDuyet,
                                decimal PhiThamTra,
                                bool HoanThanh,
                                string GhiChu)
        {
            var approve = nc.TblPheDuyets.Single(s => s.ID == id);
            approve.HoSoId = HoSoId;
            approve.DiaDiem = DiaDiem;
            approve.TGKhoiCong = TGKhoiCong;
            approve.TGHoanThanh = TGHoanThanh;
            approve.QuyMoDauTu = QuyMoDauTu;
            approve.GTQTDuocDuyet = GTQTDuocDuyet;
            approve.ChiPhiXayDung = ChiPhiXayDung;
            approve.ChiPhiThietBi = ChiPhiThietBi;
            approve.ChiPhiQLDA = ChiPhiQLDA;
            approve.ChiPhiTVDT = ChiPhiTVDT;
            approve.ChiPhiBTGPMB = ChiPhiBTGPMB;
            approve.ChiPhiKhac = ChiPhiKhac;
            approve.TrinhKy = TrinhKy;
            approve.SoLuu = SoLuu;
            approve.NgayPheDuyet = NgayPheDuyet;
            approve.VanBanPheDuyet = VanBanPheDuyet;
            approve.PhiThamTra = PhiThamTra;
            approve.HoanThanh = HoanThanh;
            approve.GhiChu = GhiChu;
            nc.SubmitChanges();
        }

        // Hàm xóa dữ liệu phê duyệt
        public void deleteApprove(int id)
        {
            var delete = nc.TblPheDuyets.Single(s => s.ID == id);
            nc.TblPheDuyets.DeleteOnSubmit(delete);
            nc.SubmitChanges();
        }
    }
}
